package com.cencat.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cencat.common.response.PageResponse;
import com.cencat.common.utils.PaginationHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;
import org.springframework.beans.BeanUtils;
import com.cencat.user.dto.RolePermissionDTO;
import com.cencat.user.dto.RolePermissionQueryDTO;
import com.cencat.user.entity.RolePermission;
import com.cencat.user.mapper.RolePermissionMapper;
import com.cencat.user.mapper.RoleMapper;
import com.cencat.user.mapper.PermissionMapper;
import com.cencat.user.service.RolePermissionService;
import com.cencat.user.vo.RolePermissionVO;
import com.cencat.common.exception.BusinessException;
import com.cencat.common.enums.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色权限关联服务实现类
 * @author cencat
 * @since 2024-01-01
 */
@Service
@Slf4j
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean assignRolePermissions(RolePermissionDTO dto) throws BusinessException {
        log.info("开始分配角色权限: roleId={}, permissionIds={}", dto.getRoleId(), dto.getPermissionIds());
        
        try {
            // 参数校验
            if (dto == null || dto.getRoleId() == null || CollectionUtils.isEmpty(dto.getPermissionIds())) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            // 检查角色是否存在
            if (roleMapper.selectById(dto.getRoleId()) == null) {
                throw new BusinessException(ErrorCode.ROLE_NOT_EXISTS);
            }

            // 检查权限是否存在
            for (Long permissionId : dto.getPermissionIds()) {
                if (permissionMapper.selectById(permissionId) == null) {
                    throw new BusinessException(ErrorCode.PERMISSION_NOT_EXISTS);
                }
            }

            // 过滤已存在的关联关系
            List<Long> newPermissionIds = new ArrayList<>();
            for (Long permissionId : dto.getPermissionIds()) {
                RolePermission existing = rolePermissionMapper.selectByRoleIdAndPermissionId(
                    dto.getRoleId(), permissionId, dto.getTenantId());
                if (existing == null) {
                    newPermissionIds.add(permissionId);
                }
            }

            if (CollectionUtils.isEmpty(newPermissionIds)) {
                log.warn("所有权限关联已存在，无需重复分配");
                return true;
            }

            // 批量插入新的关联关系
            List<RolePermission> rolePermissions = new ArrayList<>();
            LocalDateTime now = LocalDateTime.now();
            for (Long permissionId : newPermissionIds) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(dto.getRoleId());
                rolePermission.setPermissionId(permissionId);
                rolePermission.setTenantId(dto.getTenantId());
                rolePermission.setCreateTime(now);
                rolePermission.setCreateBy(dto.getOperatorId());
                rolePermissions.add(rolePermission);
            }

            int result = rolePermissionMapper.batchInsert(rolePermissions);
            if (result <= 0) {
                throw new BusinessException(ErrorCode.OPERATION_FAILED);
            }

            log.info("角色权限分配成功: roleId={}, 新增权限数量={}", dto.getRoleId(), result);
            return true;
        } catch (BusinessException e) {
            log.error("角色权限分配失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("角色权限分配异常: roleId={}, error={}", dto.getRoleId(), e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeRolePermissions(RolePermissionDTO dto) throws BusinessException {
        log.info("开始移除角色权限: roleId={}, permissionIds={}", dto.getRoleId(), dto.getPermissionIds());
        
        try {
            // 参数校验
            if (dto == null || dto.getRoleId() == null || CollectionUtils.isEmpty(dto.getPermissionIds())) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            // 检查角色是否存在
            if (roleMapper.selectById(dto.getRoleId()) == null) {
                throw new BusinessException(ErrorCode.ROLE_NOT_EXISTS);
            }

            int result = rolePermissionMapper.deleteByRoleIdAndPermissionIds(
                dto.getRoleId(), dto.getPermissionIds(), dto.getTenantId());

            log.info("角色权限移除成功: roleId={}, 移除权限数量={}", dto.getRoleId(), result);
            return true;
        } catch (BusinessException e) {
            log.error("角色权限移除失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("角色权限移除异常: roleId={}, error={}", dto.getRoleId(), e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRolePermissions(RolePermissionDTO dto) throws BusinessException {
        log.info("开始更新角色权限: roleId={}, permissionIds={}", dto.getRoleId(), dto.getPermissionIds());
        
        try {
            // 参数校验
            if (dto == null || dto.getRoleId() == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            // 检查角色是否存在
            if (roleMapper.selectById(dto.getRoleId()) == null) {
                throw new BusinessException(ErrorCode.ROLE_NOT_EXISTS);
            }

            // 先删除原有的权限关联
            rolePermissionMapper.deleteByRoleId(dto.getRoleId(), dto.getTenantId());

            // 如果有新的权限列表，则重新分配
            if (!CollectionUtils.isEmpty(dto.getPermissionIds())) {
                return assignRolePermissions(dto);
            }

            log.info("角色权限更新成功: roleId={}", dto.getRoleId());
            return true;
        } catch (BusinessException e) {
            log.error("角色权限更新失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("角色权限更新异常: roleId={}, error={}", dto.getRoleId(), e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<RolePermissionVO.PermissionInfo> getPermissionsByRoleId(Long roleId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            List<com.cencat.user.entity.Permission> permissions = rolePermissionMapper.selectPermissionsByRoleId(roleId, tenantId);
            return permissions.stream().map(permission -> {
                RolePermissionVO.PermissionInfo permissionInfo = new RolePermissionVO.PermissionInfo();
                permissionInfo.setPermissionId(permission.getId());
                permissionInfo.setPermissionName(permission.getPermissionName());
                permissionInfo.setPermissionCode(permission.getPermissionCode());
                permissionInfo.setPermissionType(permission.getPermissionType().toString());
                return permissionInfo;
            }).collect(Collectors.toList());
        } catch (BusinessException e) {
            log.error("查询角色权限失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("查询角色权限异常: roleId={}, error={}", roleId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<RolePermissionVO> getRolesByPermissionId(Long permissionId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (permissionId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.selectRolesByPermissionId(permissionId, tenantId);
        } catch (BusinessException e) {
            log.error("查询权限角色失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("查询权限角色异常: permissionId={}, error={}", permissionId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public PageResponse<RolePermissionVO> getRolePermissionPage(RolePermissionQueryDTO queryDTO) throws BusinessException {
        try {
            // 参数校验
            if (queryDTO == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            // 设置默认分页参数
            if (queryDTO.getPageNum() == null || queryDTO.getPageNum() <= 0) {
                queryDTO.setPageNum(1);
            }
            if (queryDTO.getPageSize() == null || queryDTO.getPageSize() <= 0) {
                queryDTO.setPageSize(10);
            }

            // 构建分页查询参数
            PaginationHelper.PageQuery pageQuery = new PaginationHelper.PageQuery();
            pageQuery.setCurrent(queryDTO.getPageNum().longValue());
            pageQuery.setSize(queryDTO.getPageSize().longValue());
            
            // 构建查询条件
            QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("tenant_id", queryDTO.getTenantId());
            
            if (queryDTO.getRoleId() != null) {
                queryWrapper.eq("role_id", queryDTO.getRoleId());
            }
            
            if (queryDTO.getPermissionId() != null) {
                queryWrapper.eq("permission_id", queryDTO.getPermissionId());
            }
            
            queryWrapper.eq("is_deleted", 0);
            queryWrapper.orderByDesc("create_time");
            
            // 执行分页查询并转换为VO
            return PaginationHelper.executePage(rolePermissionMapper, pageQuery, queryWrapper, this::convertToVO);
        } catch (BusinessException e) {
            log.error("分页查询角色权限关联失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("分页查询角色权限关联异常: error={}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<RolePermissionVO> getRolePermissionList(RolePermissionQueryDTO queryDTO) throws BusinessException {
        try {
            // 参数校验
            if (queryDTO == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.selectRolePermissionList(queryDTO);
        } catch (BusinessException e) {
            log.error("查询角色权限关联列表失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("查询角色权限关联列表异常: error={}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public Boolean checkRoleHasPermission(Long roleId, Long permissionId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || permissionId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.checkRoleHasPermission(roleId, permissionId, tenantId);
        } catch (BusinessException e) {
            log.error("检查角色权限失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("检查角色权限异常: roleId={}, permissionId={}, error={}", roleId, permissionId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public Boolean checkRoleHasPermissionCode(Long roleId, String permissionCode, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || permissionCode == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.checkRoleHasPermissionCode(roleId, permissionCode, tenantId);
        } catch (BusinessException e) {
            log.error("检查角色权限编码失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("检查角色权限编码异常: roleId={}, permissionCode={}, error={}", roleId, permissionCode, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<Long> getRoleIdsByPermissionIds(List<Long> permissionIds, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (CollectionUtils.isEmpty(permissionIds) || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.selectRoleIdsByPermissionIds(permissionIds, tenantId);
        } catch (BusinessException e) {
            log.error("根据权限ID查询角色ID失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("根据权限ID查询角色ID异常: error={}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<Long> getPermissionIdsByRoleIds(List<Long> roleIds, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (CollectionUtils.isEmpty(roleIds) || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.selectPermissionIdsByRoleIds(roleIds, tenantId);
        } catch (BusinessException e) {
            log.error("根据角色ID查询权限ID失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("根据角色ID查询权限ID异常: error={}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public Long countRolePermissions(Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.countRolePermissions(tenantId);
        } catch (BusinessException e) {
            log.error("统计角色权限关联数量失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("统计角色权限关联数量异常: error={}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public Long countPermissionsByRoleId(Long roleId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.countPermissionsByRoleId(roleId, tenantId);
        } catch (BusinessException e) {
            log.error("统计角色权限数量失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("统计角色权限数量异常: roleId={}, error={}", roleId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public Long countRolesByPermissionId(Long permissionId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (permissionId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.countRolesByPermissionId(permissionId, tenantId);
        } catch (BusinessException e) {
            log.error("统计权限角色数量失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("统计权限角色数量异常: permissionId={}, error={}", permissionId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteByRoleId(Long roleId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.deleteByRoleId(roleId, tenantId);
        } catch (BusinessException e) {
            log.error("根据角色ID删除权限关联失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("根据角色ID删除权限关联异常: roleId={}, error={}", roleId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteByPermissionId(Long permissionId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (permissionId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            return rolePermissionMapper.deleteByPermissionId(permissionId, tenantId);
        } catch (BusinessException e) {
            log.error("根据权限ID删除角色关联失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("根据权限ID删除角色关联异常: permissionId={}, error={}", permissionId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    public RolePermissionVO getRolePermissionDetail(Long roleId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            // 查询角色权限关联信息
            List<RolePermissionVO> rolePermissionList = rolePermissionMapper.selectRolePermissionByRoleId(roleId, tenantId);
            if (CollectionUtils.isEmpty(rolePermissionList)) {
                throw new BusinessException(ErrorCode.ROLE_NOT_EXISTS);
            }

            // 创建RolePermissionVO对象
            RolePermissionVO rolePermissionVO = rolePermissionList.get(0);

            // 查询角色权限列表
            List<com.cencat.user.entity.Permission> permissionEntities = rolePermissionMapper.selectPermissionsByRoleId(roleId, tenantId);
            List<RolePermissionVO.PermissionInfo> permissions = permissionEntities.stream()
                    .map(permission -> {
                        RolePermissionVO.PermissionInfo permissionInfo = new RolePermissionVO.PermissionInfo();
                        permissionInfo.setPermissionId(permission.getId());
                        permissionInfo.setPermissionName(permission.getPermissionName());
                        permissionInfo.setPermissionCode(permission.getPermissionCode());
                        permissionInfo.setDescription(permission.getDescription());
                        return permissionInfo;
                    })
                    .collect(Collectors.toList());
            rolePermissionVO.setPermissions(permissions);

            return rolePermissionVO;
        } catch (BusinessException e) {
            log.error("查询角色权限详情失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("查询角色权限详情异常: roleId={}, error={}", roleId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchAssignRolePermissions(List<Long> roleIds, List<Long> permissionIds, Long tenantId, Long operatorId) throws BusinessException {
        try {
            // 参数校验
            if (CollectionUtils.isEmpty(roleIds) || CollectionUtils.isEmpty(permissionIds) || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            for (Long roleId : roleIds) {
                RolePermissionDTO dto = new RolePermissionDTO();
                dto.setRoleId(roleId);
                dto.setPermissionIds(permissionIds);
                dto.setTenantId(tenantId);
                dto.setOperatorId(operatorId);
                assignRolePermissions(dto);
            }

            return true;
        } catch (BusinessException e) {
            log.error("批量分配角色权限失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("批量分配角色权限异常: error={}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchRemoveRolePermissions(List<Long> roleIds, List<Long> permissionIds, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (CollectionUtils.isEmpty(roleIds) || CollectionUtils.isEmpty(permissionIds) || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            for (Long roleId : roleIds) {
                rolePermissionMapper.deleteByRoleIdAndPermissionIds(roleId, permissionIds, tenantId);
            }

            return true;
        } catch (BusinessException e) {
            log.error("批量移除角色权限失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("批量移除角色权限异常: error={}", e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyRolePermissions(Long sourceRoleId, Long targetRoleId, Long tenantId, Long operatorId) throws BusinessException {
        try {
            // 参数校验
            if (sourceRoleId == null || targetRoleId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            // 查询源角色的权限列表
            List<RolePermission> sourcePermissions = rolePermissionMapper.selectByRoleId(sourceRoleId, tenantId);
            if (CollectionUtils.isEmpty(sourcePermissions)) {
                log.warn("源角色没有权限，无需复制: sourceRoleId={}", sourceRoleId);
                return true;
            }

            // 构建目标角色权限DTO
            List<Long> permissionIds = sourcePermissions.stream()
                .map(RolePermission::getPermissionId)
                .collect(Collectors.toList());

            RolePermissionDTO dto = new RolePermissionDTO();
            dto.setRoleId(targetRoleId);
            dto.setPermissionIds(permissionIds);
            dto.setTenantId(tenantId);
            dto.setOperatorId(operatorId);

            return assignRolePermissions(dto);
        } catch (BusinessException e) {
            log.error("复制角色权限失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("复制角色权限异常: sourceRoleId={}, targetRoleId={}, error={}", sourceRoleId, targetRoleId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean clearRolePermissions(Long roleId, Long tenantId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            rolePermissionMapper.deleteByRoleId(roleId, tenantId);
            return true;
        } catch (BusinessException e) {
            log.error("清空角色权限失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("清空角色权限异常: roleId={}, error={}", roleId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean syncRolePermissions(Long roleId, List<Long> permissionIds, Long tenantId, Long operatorId) throws BusinessException {
        try {
            // 参数校验
            if (roleId == null || tenantId == null) {
                throw new BusinessException(ErrorCode.PARAM_ERROR);
            }

            // 先清空原有权限
            clearRolePermissions(roleId, tenantId);

            // 如果有新权限，则重新分配
            if (!CollectionUtils.isEmpty(permissionIds)) {
                RolePermissionDTO dto = new RolePermissionDTO();
                dto.setRoleId(roleId);
                dto.setPermissionIds(permissionIds);
                dto.setTenantId(tenantId);
                dto.setOperatorId(operatorId);
                return assignRolePermissions(dto);
            }

            return true;
        } catch (BusinessException e) {
            log.error("同步角色权限失败: {}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("同步角色权限异常: roleId={}, error={}", roleId, e.getMessage(), e);
            throw new BusinessException(ErrorCode.OPERATION_FAILED);
        }
    }

    /**
     * 将RolePermission实体转换为RolePermissionVO
     * @param rolePermission 角色权限关联实体
     * @return RolePermissionVO
     */
    private RolePermissionVO convertToVO(RolePermission rolePermission) {
        if (rolePermission == null) {
            return null;
        }
        
        RolePermissionVO vo = new RolePermissionVO();
        BeanUtils.copyProperties(rolePermission, vo);
        return vo;
    }
}